<template>

  <div class="add-or-update">
  <el-form :model="paramForm" ref="paramForm" :rules="dataRule"  label-width="100px">
    <el-form-item label="参数代码:" prop="code">
      <el-input v-model="paramForm.code" placeholder="请输入参数代码"></el-input>
    </el-form-item>
    <el-form-item label="名称:" prop="name">
      <el-input v-model="paramForm.name" placeholder="请输入名称"></el-input>
    </el-form-item>
    <el-form-item label="值:" prop="value">
      <el-input v-model="paramForm.value" placeholder="请输入参数值"></el-input>
    </el-form-item>
    <el-form-item label="备注:" prop="remarks">
      <el-input type="textarea"  placeholder="请输入备注" :rows=4 v-model="paramForm.remarks"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm()">保存</el-button>
      <el-button @click="resetForm('paramForm')">重置</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        loading: false,
        paramForm: {
          id: '',
          name: '',
          code: '',
          value: '',
          remarks: ''
        },
        dataRule: {
          code: [
            {required: true, message: '代码不能为空', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ],
          value: [
            {required: true, message: '值不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    activated () {
      let id = this.$route.params.id || 0
      console.log('id', id)
      this.paramForm.id = id
      this.init(id)
    },
    methods: {
      init (id) {
        this.paramForm.id = id || 0
        console.log(this.paramForm.id)
        if (this.paramForm.id) {
          this.loading = true
            // 修改
          this.$http({
            url: this.$http.adornUrl(`/sysParam/query/detail`),
            method: 'get',
            params: this.$http.adornParams({id: id})
          }).then(({data}) => {
            this.loading = false
            this.paramForm.id = data.id
            this.paramForm.name = data.name
            this.paramForm.code = data.code
            this.paramForm.value = data.value
            this.paramForm.remarks = data.remarks
          })
        } else {
            // 新增
          this.resetForm('paramForm')
        }
      },
      submitForm () {
        this.$refs['paramForm'].validate(valid => {
          if (valid) {
            this.loading = true
            this.$http({
              url: this.$http.adornUrl('/sysParam/save'),
              method: 'post',
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
              },
              data: this.$http.adornData(
                {
                  id: this.paramForm.id || undefined,
                  name: this.paramForm.name,
                  code: this.paramForm.code,
                  value: this.paramForm.value,
                  remarks: this.paramForm.remarks
                },
                false,
                'form'
              )
            }).then(() => {
              this.loading = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.$router.push({name: 'sys-param'})
            })
          }
        })
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
      }

    }
  }
</script>
